//
//  JiaZaiAnimationHandle.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/5/17.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "JiaZaiAnimationHandle.h"
#import "ZGWAddTitleWithAnimation.h"

@interface JiaZaiAnimationHandle ()

@property (nonatomic, strong) UIImageView *animationImageV; // 动画
@property (nonatomic, strong) UIView *topGrayView; // 提示框

@end

@implementation JiaZaiAnimationHandle

+ (JiaZaiAnimationHandle *)shareJiaZaiAnimationHandle
{
    static JiaZaiAnimationHandle *jiaZaiHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        jiaZaiHandle = [[JiaZaiAnimationHandle alloc] init];
        
        jiaZaiHandle.animationImageV = [[UIImageView alloc] initWithFrame:CGRectMake((375 - 50) / 2 * kAdjustParamater, 300 * kAdjustParamater, 50 * kAdjustParamater, 50 * kAdjustParamater)];
        
            });
    return jiaZaiHandle;
}

// 开始动画
- (void)startAnimation
{
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    [win addSubview:_animationImageV];
    self.animationImageV.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"加载60x60-01"], [UIImage imageNamed:@"加载60x60-02"], [UIImage imageNamed:@"加载60x60-03"], [UIImage imageNamed:@"加载60x60-04"], [UIImage imageNamed:@"加载60x60-05"], [UIImage imageNamed:@"加载60x60-06"], [UIImage imageNamed:@"加载60x60-07"], [UIImage imageNamed:@"加载60x60-08"], nil];
    self.animationImageV.animationRepeatCount = 0;
    self.animationImageV.animationDuration = 1.5;
//    UIWindow *win = [UIApplication sharedApplication].keyWindow;
//    self.animationImageV = [[UIImageView alloc] initWithFrame:CGRectMake((375 - 50) / 2 * kAdjustParamater, 300 * kAdjustParamater, 50 * kAdjustParamater, 50 * kAdjustParamater)];
//    self.animationImageV.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"加载60x60-01"], [UIImage imageNamed:@"加载60x60-02"], [UIImage imageNamed:@"加载60x60-03"], [UIImage imageNamed:@"加载60x60-04"], [UIImage imageNamed:@"加载60x60-05"], [UIImage imageNamed:@"加载60x60-06"], [UIImage imageNamed:@"加载60x60-07"], [UIImage imageNamed:@"加载60x60-08"], nil];
//    self.animationImageV.animationRepeatCount = 0;
//    self.animationImageV.animationDuration = 1.5;
//    [win addSubview:_animationImageV];
//      self.animationImageV.hidden = NO;
      [self.animationImageV startAnimating];
    
}

// 结束动画
- (void)stopAnimation
{
 //   self.animationImageV.hidden = YES;
    [self.animationImageV stopAnimating];
//    [self.animationImageV stopAnimating];
//    [self.animationImageV removeFromSuperview];
}

// 添加提醒框
- (void)addAlertTitle:(NSString *)title Content:(NSString *)content ToView:(UIViewController *)viewVC
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:title];
    [titleString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [titleString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertVC setValue:titleString forKey:@"attributedTitle"];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:sureAction];
    [viewVC presentViewController:alertVC animated:YES completion:nil];
    
    // 默认3秒以后消失
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [viewVC dismissViewControllerAnimated:YES completion:nil];
    });
}


// 添加无标题的提醒框
- (void)addAlertNoTitieWithContent:(NSString *)content toView:(UIViewController *)viewVC
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改message
    NSMutableAttributedString *messageString = [[NSMutableAttributedString alloc] initWithString:content];
    [messageString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, messageString.length)];
    [messageString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:k17Big] range:NSMakeRange(0, messageString.length)];
    [alertVC setValue:messageString forKey:@"attributedMessage"];
    [viewVC presentViewController:alertVC animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [viewVC dismissViewControllerAnimated:YES completion:nil];
    });

}

// 添加上方灰色条提醒框
- (void)addTopGrayAlertWithContent:(NSString *)content toView:(UIViewController *)viewVC
{
    self.topGrayView = [[UIView alloc] initWithFrame:CGRectZero];
    if (viewVC.navigationController.navigationBar.translucent == YES) {
        // 说明需要上调
        self.topGrayView.frame = CGRectMake((kScreenWidth - 350 * kAdjustParamater) / 2, 10 * kAdjustParamater + 64, 350 * kAdjustParamater, 45 * kAdjustParamater);
    } else {
        self.topGrayView.frame = CGRectMake((kScreenWidth - 350 * kAdjustParamater) / 2, 10 * kAdjustParamater, 350 * kAdjustParamater, 45 * kAdjustParamater);
    }
//    self.topGrayView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth - 350 * kAdjustParamater) / 2, 10 * kAdjustParamater, 350 * kAdjustParamater, 45 * kAdjustParamater)];
    self.topGrayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [viewVC.view addSubview:_topGrayView];
    
  
    
    UIImageView *noConditonImageV = [[UIImageView alloc] initWithFrame:CGRectMake(80 * kAdjustParamater, 12 * kAdjustParamater, 20 * kAdjustParamater, 20 * kAdjustParamater)];
    noConditonImageV.image = [UIImage imageNamed:@"noContidion-teshusousuo"];
    [self.topGrayView addSubview:noConditonImageV];
    UILabel *noConditonLabel = [[UILabel alloc] initWithFrame:CGRectMake(noConditonImageV.right + 2 * kAdjustParamater, 13 * kAdjustParamater, 180 * kAdjustParamater, 18 * kAdjustParamater)];
    //   noConditonLabel.backgroundColor = [UIColor redColor];
    noConditonLabel.text = content;
    noConditonLabel.textColor = [UIColor whiteColor];
    noConditonLabel.font = [UIFont systemFontOfSize:17 * kAdjustParamater];
    [self.topGrayView addSubview:noConditonLabel];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.topGrayView removeFromSuperview];
    });
    
}

// 添加上方灰色条提醒框没有图片
- (void)addTopGrayAlertNoImageWithContent:(NSString *)content toView:(UIViewController *)viewVC
{
    self.topGrayView = [[UIView alloc] initWithFrame:CGRectZero];
    if (viewVC.navigationController.navigationBar.translucent == YES) {
        // 说明需要上调
        self.topGrayView.frame = CGRectMake((kScreenWidth - 350 * kAdjustParamater) / 2, 10 * kAdjustParamater + 64, 350 * kAdjustParamater, 45 * kAdjustParamater);
    } else {
        self.topGrayView.frame = CGRectMake((kScreenWidth - 350 * kAdjustParamater) / 2, 10 * kAdjustParamater, 350 * kAdjustParamater, 45 * kAdjustParamater);
    }
    //    self.topGrayView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth - 350 * kAdjustParamater) / 2, 10 * kAdjustParamater, 350 * kAdjustParamater, 45 * kAdjustParamater)];
    self.topGrayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [viewVC.view addSubview:_topGrayView];
    
    
    
    UILabel *noConditonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 13 * kAdjustParamater, 350 * kAdjustParamater, 18 * kAdjustParamater)];
    //   noConditonLabel.backgroundColor = [UIColor redColor];
    noConditonLabel.text = content;
    noConditonLabel.textColor = [UIColor whiteColor];
    noConditonLabel.textAlignment = NSTextAlignmentCenter;
    noConditonLabel.font = [UIFont systemFontOfSize:17 * kAdjustParamater];
    [self.topGrayView addSubview:noConditonLabel];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.topGrayView removeFromSuperview];
    });
}

// 添加不消失的提醒框
- (void)addNoDismissAlertWithTitle:(NSString *)titleString AndContent:(NSString *)content toView:(UIViewController *)viewVC
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:titleString message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:titleString];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, title.length)];
    [alertVC setValue:title forKey:@"attributedTitle"];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:sureAction];
    [viewVC presentViewController:alertVC animated:YES completion:nil];
}


// 提示框有动画
- (void)addAlertWithAnimationByTitle:(NSString *)title toView:(UIViewController *)viewVC
{
    if (viewVC.navigationController.navigationBar.translucent == YES)  {
        NSLog(@"导航栏透明");
        ZGWAddTitleWithAnimation *customView = [[ZGWAddTitleWithAnimation alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 350 * kAdjustParamater) withTitle:title];
        customView.backgroundColor = [UIColor clearColor];
        customView.transform = CGAffineTransformMakeTranslation(0, 600 * kAdjustParamater);
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            customView.transform = CGAffineTransformIdentity;
            [viewVC.view addSubview:customView];
        } completion:^(BOOL finished) {
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [customView removeFromSuperview];
        });
    } else if (viewVC.navigationController.navigationBar.translucent == NO) {
        NSLog(@"导航栏不透明");
        ZGWAddTitleWithAnimation *customView = [[ZGWAddTitleWithAnimation alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 350 * kAdjustParamater) withTitle:title];
        customView.backgroundColor = [UIColor clearColor];
        customView.transform = CGAffineTransformMakeTranslation(0, 600 * kAdjustParamater);
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            customView.transform = CGAffineTransformIdentity;
            [viewVC.view addSubview:customView];
        } completion:^(BOOL finished) {
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [customView removeFromSuperview];
        });
    }
  
}

// 提示框有动画有时间
- (void)addAlertWithAnimationByTitle:(NSString *)title  withAniationTime:(NSInteger)time toView:(UIViewController *)viewVC
{
    if (viewVC.navigationController.navigationBar.translucent == YES) {
        NSLog(@"导航栏透明");
        ZGWAddTitleWithAnimation *customView = [[ZGWAddTitleWithAnimation alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 350 * kAdjustParamater) withTitle:title animationDisplayTime:time];
        customView.backgroundColor = [UIColor clearColor];
        customView.transform = CGAffineTransformMakeTranslation(0, 600 * kAdjustParamater);
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            customView.transform = CGAffineTransformIdentity;
            [viewVC.view addSubview:customView];
        } completion:^(BOOL finished) {
        }];
    } else if (viewVC.navigationController.navigationBar.translucent == NO) {
        NSLog(@"导航栏不透明");
        ZGWAddTitleWithAnimation *customView = [[ZGWAddTitleWithAnimation alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 350 * kAdjustParamater) withTitle:title animationDisplayTime:time];
        customView.backgroundColor = [UIColor clearColor];
        customView.transform = CGAffineTransformMakeTranslation(0, 600 * kAdjustParamater);
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            customView.transform = CGAffineTransformIdentity;
            [viewVC.view addSubview:customView];
        } completion:^(BOOL finished) {
        }];
    }
    
}

@end
